#include "StdAfx.h"
#include "Listview.h"
#include "Resource.h"


#include <shlwapi.h>
#pragma comment(lib,"Shlwapi.lib")
#include <shellapi.h>
#include <fstream>
#include <shlobj.h>

using namespace std;
Listview::Listview(void)
{
}

Listview::~Listview(void)
{
}
HWND Listview::CreateListView(int styles, int x, int y, int width, int height, HWND hWndParent, int id)
{
	InitCommonControls();
	hList = CreateWindow(WC_LISTVIEW,NULL,styles,x,y,width,height,hWndParent,(HMENU)id,hInst,NULL);
	return hList;
}
BOOL Listview::InitColumn()
{
	LVCOLUMN column;
	column.mask = LVCF_FMT|LVCF_TEXT | LVCF_WIDTH |LVCF_SUBITEM |LVCFMT_IMAGE;
	column.cx= 200;
	column.fmt = LVCFMT_RIGHT | LVCFMT_IMAGE;
	column.pszText = _T("Name");
	ListView_InsertColumn(hList,0,&column);

	column.fmt = LVCFMT_LEFT;
	column.pszText = _T("Status");
	column.cx= 100;
	column.iSubItem = 1;
	ListView_InsertColumn(hList,1,&column);



	
	return  true;
}

BOOL Listview::initImageList()
{
	InitCommonControls();
	HIMAGELIST hMIlist,hLIlist;
	Shell_GetImageLists(&hLIlist,&hMIlist);
	ListView_SetImageList(hList,hMIlist,LVSIL_SMALL);
	//ListView_SetImageList(hList,hMIlist,LVSIL_NORMAL);
	return TRUE;
}

BOOL Listview::AddItem( LPTSTR text, LPTSTR score,LPTSTR status,int iRow)
{
	LVITEM item;
	TCHAR textsize[40];
	
	item.mask = LVIF_IMAGE | LVIF_STATE | LVIF_TEXT | LVIF_PARAM ;
	item.state = 0  ;
	item.stateMask =  0;
	item.iItem = iRow;
	item.lParam = 0;
	
	item.pszText = text;
	item.iSubItem = 0;
	int nItem = ListView_InsertItem(hList,&item);


	SetItem(nItem,1,status);
	SetItem(nItem,2,score);

	return TRUE;
}

void Listview::SetItem(int nItem, int iSubItem, LPTSTR text)
{
	LVITEM item;
	item.mask = LVIF_TEXT;
	item.iItem = nItem;
	item.iSubItem = iSubItem;
	item.pszText = text;
	ListView_SetItem(hList,&item);
}


void Listview::DeleteAllItem()
{
	SendMessage(hList,LVM_DELETEALLITEMS,0,0);
}

void Listview::GetSelItem(LPTSTR text)
{
	
	int indexl = SendMessage(hList, LVM_GETSELECTIONMARK,0,0);

	LVITEM item;
	item.mask =LVIF_TEXT ;
	item.iItem = indexl;	
	item.pszText = text;		
	item.iSubItem = 0;
	item.cchTextMax = 260;
	SendMessage(hList,LVM_GETITEM,0,(LPARAM)&item);
}


void Listview::MakeTextTime(LPFILETIME ft, LPTSTR temp)
{
	FILETIME localtime;
	SYSTEMTIME systime;
	
	BOOL AM = TRUE;
	FileTimeToLocalFileTime(ft,&localtime);
	FileTimeToSystemTime(&localtime,&systime);
	if(systime.wHour>12)
	{
		systime.wHour-=12;
		AM = FALSE;
	}
	_stprintf(temp,_T("%d/%d/%d %d:%d "),systime.wMonth,systime.wDay,systime.wYear,systime.wHour,systime.wMinute);
	if(AM == FALSE)
		wcscat(temp,_T("PM"));
	else
		wcscat(temp,L"AM");

	
}
